package forward

import (
	"encoding/json"
	"gitee.com/lstack_light/light-utils/common"
	res "gitee.com/lstack_light/light-utils/common/response"
	"github.com/go-resty/resty/v2"
	"net/http"
)

var (
	DefaultClient = defaultRestClient()
)

type Forward struct {
	*resty.Client
}

// Query 参数描述：query参数
type Query struct {
	// Headers 参数描述：请求头
	Headers map[string]string
	// Params 参数描述：请求参数
	Params map[string]string
	// BaseUrl 参数描述：请求地址
	BaseUrl string
}

// Body 参数描述：body参数
type Body struct {
	// Headers 参数描述：请求头
	Headers map[string]string
	// Params 参数描述：请求参数
	Params interface{}
	// BaseUrl 参数描述：请求地址
	BaseUrl string
}

// defaultRestClient
/**
 * 功能描述：初始化默认客户端请求
 * @author KangXu
 * @return *Forward
 */
func defaultRestClient() *Forward {
	return &Forward{Client: resty.New().SetBaseURL(common.DefaultBaseUrl)}
}

// NewRestClient
/**
 * 功能描述：初始化新建客户端请求
 * @author KangXu
 * @param serverName 调用的服务名称
 * @param userAgent 服务端的密钥
 * @return *Forward
 */
func NewRestClient(serverName, userAgent string) *Forward {
	DefaultClient.SetHeaders(map[string]string{
		common.HeaderServer: serverName,
		common.AgentSecret:  userAgent,
	})
	return DefaultClient
}

// SetBaseUrl
/**
 * 功能描述：设置请求地址
 * @author KangXu
 * @param url 请求地址
 * @return *Forward
 */
func (f *Forward) SetBaseUrl(url string) *Forward {
	client := f.SetBaseURL(url)
	return &Forward{Client: client}
}

// SetHeader
/**
 * 功能描述：设置请求头
 * @author KangXu
 * @param headers 请求头
 * @return *Forward
 */
func (f *Forward) SetHeader(headers map[string]string) *Forward {
	client := f.SetHeaders(headers)
	return &Forward{Client: client}
}

// Get
/**
 * 功能描述：发送get请求
 * @author KangXu
 * @param param get请求的相关参数
 * @return requestErr error, responseResult res.Response
 */
func (f *Forward) Get(param *Query) (requestErr error, responseResult res.Response) {
	response, err := f.
		R().
		SetHeaders(param.Headers).
		SetQueryParams(param.Params).
		Get(param.BaseUrl)
	if nil != err {
		requestErr = err
		return
	}
	if 0 < len(response.Body()) {
		err = json.Unmarshal(response.Body(), &responseResult)
		if nil != err {
			responseResult.Code = http.StatusInternalServerError
			responseResult.Message = "映射响应体异常--" + err.Error()
		}
	}
	return
}

// Post
/**
 * 功能描述：发送post请求
 * @author KangXu
 * @param param post请求的相关参数
 * @return requestErr error, responseResult res.Response
 */
func (f *Forward) Post(param *Body) (requestErr error, responseResult res.Response) {
	response, err := f.
		R().
		SetHeaders(param.Headers).
		SetBody(param.Params).
		Post(param.BaseUrl)
	if nil != err {
		requestErr = err
		return
	}
	if 0 < len(response.Body()) {
		err = json.Unmarshal(response.Body(), &responseResult)
		if nil != err {
			responseResult.Code = http.StatusInternalServerError
			responseResult.Message = "映射响应体异常--" + err.Error()
		}
	}
	return
}

// Put
/**
 * 功能描述：发送put请求
 * @author KangXu
 * @param param put请求的相关参数
 * @return requestErr error, responseResult res.Response
 */
func (f *Forward) Put(param *Query) (requestErr error, responseResult res.Response) {
	response, err := f.
		R().
		SetHeaders(param.Headers).
		SetQueryParams(param.Params).
		Put(param.BaseUrl)
	if nil != err {
		requestErr = err
		return
	}
	if 0 < len(response.Body()) {
		err = json.Unmarshal(response.Body(), &responseResult)
		if nil != err {
			responseResult.Code = http.StatusInternalServerError
			responseResult.Message = "映射响应体异常--" + err.Error()
		}
	}
	return
}

// Delete
/**
 * 功能描述：发送delete请求
 * @author KangXu
 * @param param delete请求的相关参数
 * @return requestErr error, responseResult res.Response
 */
func (f *Forward) Delete(param *Body) (requestErr error, responseResult res.Response) {
	response, err := f.
		R().
		SetHeaders(param.Headers).
		SetBody(param.Params).
		Delete(param.BaseUrl)
	if nil != err {
		requestErr = err
		return
	}
	if 0 < len(response.Body()) {
		err = json.Unmarshal(response.Body(), &responseResult)
		if nil != err {
			responseResult.Code = http.StatusInternalServerError
			responseResult.Message = "映射响应体异常--" + err.Error()
		}
	}
	return
}
